package com.watson.onebox.algorithm.descarte;

import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    private BeanMap beanMap = null; //存放属性名称以及属性的类型

    public static void main(String[] args) {
        String ab = "abb";
        String ad = ab + "";
        TableVo tableVo = new TableVo();
        Map<String, List<Object>> params = new HashMap<>();
        List<Object> erp = new ArrayList<>();
        Field[] declaredFields = TableVo.class.getDeclaredFields();
        declaredFields[0].getName();
        params.put("erp", erp);

        String name = declaredFields[0].getName();

        getparams(TableVo.class, null);

        List<String> list1 = new ArrayList<String>();
        list1.add("a");
        list1.add("b");
        List<String> list2 = new ArrayList<String>();
        list2.add("0");
        list2.add("1");
        list2.add("2");
        List<List<String>> dimValue = new ArrayList<List<String>>();
        dimValue.add(list1);
        dimValue.add(list2);

        // 递归实现笛卡尔积
        List<List<String>> res = descartes(dimValue);
        System.out.println("递归实现笛卡尔乘积: 共 " + res.size() + " 个结果");
        for (List<String> list : res) {
            for (String string : list) {
                System.out.print(string + " ");
            }
            System.out.println();
        }

        for (int i = 0; i < aa.length * bb.length * cc.length; i++) {
            System.out.print(aa[counter[0]]);
            System.out.print("\t");
            System.out.print(bb[counter[1]]);
            System.out.print("\t");
            System.out.print(cc[counter[2]]);
            System.out.println();
            handle();
        }
        System.out.println();
    }

    public static void testMethod() {

    }

    public static void getparams(Class claz, Map<String, List<Object>> params) {
        Field[] fields = claz.getDeclaredFields();
        Class<?> type = fields[0].getType();
        Class[] types = new Class[]{claz};
        try {
            Object o = types[0].newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        System.out.println(fields.toString());

    }

    private static void backtrace(List<List<String>> dimValue, int index,
                           List<List<String>> result, List<String> curList) {

        if (curList.size() == dimValue.size())
            result.add(new ArrayList<>(curList));
        else
            for (int j = 0; j < dimValue.get(index).size(); j++) {
                curList.add(dimValue.get(index).get(j));
                backtrace(dimValue, index + 1, result, curList);
                curList.remove(curList.size() - 1);
            }

    }


    public static List<List<String>> descartes(List<List<String>> dimValue) {
        List<List<String>> res = new ArrayList<>();
        if (dimValue == null || dimValue.size() == 0)
            return res;
        backtrace(dimValue, 0, res, new ArrayList<>());
        return res;

    }

    private static String[] aa = { "aa1", "aa2" };
    private static String[] bb = { "bb1", "bb2", "bb3" };
    private static String[] cc = { "cc1", "cc2", "cc3", "cc4" };
    private static String[][] xyz = { aa, bb, cc };
    private static int counterIndex = xyz.length - 1;
    private static int[] counter = { 0, 0, 0 };

    public static void handle() {
        counter[counterIndex]++;
        if (counter[counterIndex] >= xyz[counterIndex].length) {
            counter[counterIndex] = 0;
            counterIndex--;
            if (counterIndex >= 0) {
                handle();
            }
            counterIndex = xyz.length - 1;
        }
    }

}
